package com.xbongbong.paas.help;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormExplainEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.model.PaasFormExplainModel;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.formdata.pojo.dto.SaasAnalysisDataDTO;
import com.xbongbong.saas.enums.RedundantTemplateTypeEnum;
import com.xbongbong.saas.enums.VisibleRangeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.model.AssembleModel;
import com.xbongbong.saas.model.BomBillModel;
import com.xbongbong.saas.model.ClueModel;
import com.xbongbong.saas.model.CommunicatePlanModel;
import com.xbongbong.saas.model.ContactModel;
import com.xbongbong.saas.model.ContractModel;
import com.xbongbong.saas.model.CostAdjustModel;
import com.xbongbong.saas.model.CreditFixedLimitModel;
import com.xbongbong.saas.model.CreditTemporaryLimitModel;
import com.xbongbong.saas.model.CustomerCommunicateModel;
import com.xbongbong.saas.model.CustomerModel;
import com.xbongbong.saas.model.DistributorAccountModel;
import com.xbongbong.saas.model.InstockModel;
import com.xbongbong.saas.model.InventoryModel;
import com.xbongbong.saas.model.InvoiceModel;
import com.xbongbong.saas.model.OpportunityModel;
import com.xbongbong.saas.model.OtherExpenseModel;
import com.xbongbong.saas.model.OtherIncomeModel;
import com.xbongbong.saas.model.OutstockModel;
import com.xbongbong.saas.model.PayPlanModel;
import com.xbongbong.saas.model.PayPlanSheetModel;
import com.xbongbong.saas.model.PaymentModel;
import com.xbongbong.saas.model.PaymentSheetModel;
import com.xbongbong.saas.model.PaymentTaskModel;
import com.xbongbong.saas.model.ProductModel;
import com.xbongbong.saas.model.ProductionOrderModel;
import com.xbongbong.saas.model.PurchaseInvoiceModel;
import com.xbongbong.saas.model.PurchaseModel;
import com.xbongbong.saas.model.QuotationModel;
import com.xbongbong.saas.model.RefundModel;
import com.xbongbong.saas.model.ReturnedPurchaseModel;
import com.xbongbong.saas.model.SupplierCommunicateModel;
import com.xbongbong.saas.model.SupplierContactModel;
import com.xbongbong.saas.model.SupplierModel;
import com.xbongbong.saas.model.TransferModel;
import com.xbongbong.saas.model.WarehouseModel;
import com.xbongbong.saas.model.WorkOrderModel;
import com.xbongbong.saas.model.WorkReportModel;
import com.xbongbong.saas.util.UserFieldUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
public class FormatDataHelp {

    private static final Logger LOG = LoggerFactory.getLogger(FormatDataHelp.class);

    @Resource
    private ContactModel contactModel;
    @Resource
    private OutstockModel outstockModel;
    @Resource
    private OpportunityModel opportunityModel;
    @Resource
    private WorkReportModel workReportModel;
    @Resource
    private CommunicatePlanModel communicatePlanModel;
    @Resource
    private BomBillModel bomBillModel;
    @Resource
    private CustomerCommunicateModel customerCommunicateModel;
    @Resource
    private SupplierCommunicateModel supplierCommunicateModel;
    @Resource
    private ReturnedPurchaseModel returnedPurchaseModel;
    @Resource
    private CustomerModel customerModel;
    @Resource
    private InventoryModel inventoryModel;
    @Resource
    private TransferModel transferModel;
    @Resource
    private WarehouseModel warehouseModel;
    @Resource
    private SupplierModel supplierModel;
    @Resource
    private SupplierContactModel supplierContactModel;
    @Resource
    private PurchaseModel purchaseModel;
    @Resource
    private ProductionOrderModel productionOrderModel;
    @Resource
    private ContractModel contractModel;
    @Resource
    private InvoiceModel invoiceModel;
    @Resource
    private PayPlanModel payPlanModel;
    @Resource
    private PaymentModel paymentModel;
    @Resource
    private ProductModel productModel;
    @Resource
    private AssembleModel assembleModel;
    @Resource
    private InstockModel instockModel;
    @Resource
    private RefundModel refundModel;
    @Resource
    private PaymentSheetModel paymentSheetModel;
    @Resource
    private WorkOrderModel workOrderModel;
    @Resource
    private QuotationModel quotationModel;
    @Resource
    private PayPlanSheetModel payPlanSheetModel;
    @Resource
    private PurchaseInvoiceModel purchaseInvoiceModel;
    @Resource
    private ClueModel clueModel;
    @Resource
    private PaymentTaskModel paymentTaskModel;
    @Resource
    private CreditTemporaryLimitModel creditTemporaryLimitModel;
    @Resource
    private CreditFixedLimitModel creditFixedLimitModel;
    @Resource
    private DistributorAccountModel distributorAccountModel;
    @Resource
    private OtherIncomeModel otherIncomeModel;
    @Resource
    private OtherExpenseModel otherExpenseModel;
    @Resource
    private CostAdjustModel costAdjustModel;
    @Resource
    private BusinessUserTeamHelp businessUserTeamHelp;
    @Resource
    private PaasFormExplainModel paasFormExplainModel;


    /**
     * saas特殊字段格式化
     *
     * @param saasAnalysisDataDTO 数据对象
     * @throws XbbException 抛出业务异常
     * @author haibin.zhang
     */
    public void formatData(SaasAnalysisDataDTO saasAnalysisDataDTO, JSONObject result) throws XbbException {
        Integer businessType = XbbRefTypeEnum.transferBusinessType4Distributor(saasAnalysisDataDTO.getBusinessType(), saasAnalysisDataDTO.getDistributorMark());
        if (businessType == null || businessType == 0) {
            LOG.error("businessType为空");
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100002, SystemErrorCodeEnum.API_ERROR_100002.getMsg());
        }
        RedundantTemplateTypeEnum templateTypeEnum = RedundantTemplateTypeEnum.getByCode(businessType);
        switch (templateTypeEnum) {
            case CUSTOMER:
                customerModel.formatData(saasAnalysisDataDTO, result);
                break;
            case CONTACT:
                contactModel.formatData(saasAnalysisDataDTO, result);
                break;
            case CONTRACT:
                contractModel.formatData(saasAnalysisDataDTO, result);
                break;
            case OUTSTOCK:
            case CONTRACT_OUTSTOCK:
            case PRODUCTION_MATERIEL_OUTSTOCK:
            case WORK_ORDER_OUTSTOCK:
            case OTHER_OUTSTOCK:
            case RETURNED_PURCHASE_OUTSTOCK:
                outstockModel.formatData(saasAnalysisDataDTO, result);
                break;
            case INSTOCK:
            case PURCHASE_INSTOCK:
            case PRODUCTION_INSTOCK:
            case OTHER_INSTOCK:
            case RETURNED_MATERIEL_INSTOCK:
            case REFUND_INSTOCK:
                instockModel.formatData(saasAnalysisDataDTO, result);
                break;
            case SUPPLIER:
                supplierModel.formatData(saasAnalysisDataDTO, result);
                break;
            case SUPPLIER_CONTACT:
                supplierContactModel.formatData(saasAnalysisDataDTO, result);
                break;
            case PURCHASE:
                purchaseModel.formatData(saasAnalysisDataDTO, result);
                break;
            case RETURNED_PURCHASE:
                returnedPurchaseModel.formatData(saasAnalysisDataDTO, result);
                break;
            case SALES_OPPORTUNITY:
                opportunityModel.formatData(saasAnalysisDataDTO, result);
                break;
            case WORKREPORT_DAILY:
            case WORKREPORT_WEEKLY:
            case WORKREPORT_MONTHLY:
            case WORKREPORT:
                workReportModel.formatData(saasAnalysisDataDTO, result);
                break;
            case COMMUNICATE_PLAN:
                communicatePlanModel.formatData(saasAnalysisDataDTO, result);
                break;
            case BOMBILL:
                bomBillModel.formatData(saasAnalysisDataDTO, result);
                break;
            case JXC_COMMUNICATE:
                supplierCommunicateModel.formatData(saasAnalysisDataDTO, result);
                break;
            case COMMUNICATE:
                customerCommunicateModel.formatData(saasAnalysisDataDTO, result);
                break;
            case INVENTORY:
                inventoryModel.formatData(saasAnalysisDataDTO, result);
                break;
            case TRANSFER:
                transferModel.formatData(saasAnalysisDataDTO, result);
                break;
            case WAREHOUSE:
                warehouseModel.formatData(saasAnalysisDataDTO, result);
                break;
            case PRODUCT:
                productModel.formatData(saasAnalysisDataDTO, result);
                break;
            case PRODUCTIONORDER:
                productionOrderModel.formatData(saasAnalysisDataDTO, result);
                break;
            case INVOICE:
                invoiceModel.formatData(saasAnalysisDataDTO, result);
                break;
            case PURCHASE_INVOICE:
                purchaseInvoiceModel.formatData(saasAnalysisDataDTO,result);
                break;
            case ASSEMBLE:
                assembleModel.formatData(saasAnalysisDataDTO, result);
                break;
            case PAY_PLAN:
                payPlanModel.formatData(saasAnalysisDataDTO, result);
                break;
            case PAYMENT:
                paymentModel.formatData(saasAnalysisDataDTO, result);
                break;
            case PAY_SHEET:
                payPlanSheetModel.formatData(saasAnalysisDataDTO, result);
                break;
            case PAYMENT_SHEET:
                paymentSheetModel.formatData(saasAnalysisDataDTO, result);
                break;
            case REFUND:
                refundModel.formatData(saasAnalysisDataDTO, result);
                break;
            case WORKORDER:
                workOrderModel.formatData(saasAnalysisDataDTO, result);
                break;
            case QUOTATION:
                quotationModel.formatData(saasAnalysisDataDTO, result);
                break;
            case CLUE:
                clueModel.formatData(saasAnalysisDataDTO, result);
                break;
            case PAYMENT_TASK:
                paymentTaskModel.formatData(saasAnalysisDataDTO, result);
                break;
            case CREDIT_TEMPORARY_LIMIT:
                creditTemporaryLimitModel.formatData(saasAnalysisDataDTO, result);
                break;
            case CREDIT_FIXED_LIMIT:
                creditFixedLimitModel.formatData(saasAnalysisDataDTO, result);
                break;
            case DISTRIBUTOR_MANAGEMENT:
                break;
            case DISTRIBUTOR_ACCOUNT:
                distributorAccountModel.formatData(saasAnalysisDataDTO, result);
                break;
            case DISTRIBUTOR_CONTACT:
                contactModel.formatDataForDistributor(saasAnalysisDataDTO, result);
                break;
            case DISTRIBUTOR_COMMUNICATE:
                customerCommunicateModel.formatDataForDistributor(saasAnalysisDataDTO, result);
                break;
            case ORDER:
                contractModel.formatDataForDistributor(saasAnalysisDataDTO,result);
                break;
            case ORDER_OUTSTOCK:
                outstockModel.formatDataForDistributor(saasAnalysisDataDTO,result);
                break;
            case RETURN:
                refundModel.formatDataForDistributor(saasAnalysisDataDTO,result);
                break;
            case OTHER_INCOME:
                otherIncomeModel.formatData(saasAnalysisDataDTO, result);
                break;
            case OTHER_EXPENSE:
                otherExpenseModel.formatData(saasAnalysisDataDTO, result);
                break;
            case COST_ADJUST:
                costAdjustModel.formatData(saasAnalysisDataDTO, result);
                break;
            case SERVICE_PROJECT:
                break;
            default:
                break;
        }
    }
}
